<template>
  <a-modal v-model:visible="props.show" title="编辑">
    <template #footer>
      <a-button key="back" @click="cancel">返回</a-button>
      <a-button key="submit" type="primary" @click="changeData">保存</a-button>
    </template>
    <a-form layout="inline">
      <FormInput label="分类名称" type="text" v-model:value="data.name" />
    </a-form>
  </a-modal>
</template>
<script setup lang="ts">
import { defineEmits, reactive, ref, watch } from "vue";
import FormInput from "@/components/FormInput/FormInput.vue";
import FormSelect from "@/components/FormSelect/FormSelect.vue";
import { EnumToDicData } from "@/entity/helper/enum.helper";
import { Config, EventBus, Service } from "@/main";
import { showAlert } from "@/entity/helper/tips.helper";
const emit = defineEmits(["update:show", "update:finish"]);
const props = defineProps<{
  data: {
    name: string;
    id: string;
  };
  show: boolean;
}>();
const data = reactive(props.data);
const changeData = () => {
  if (data.id === "") {
    //post创建
    Service.Shop.addCategory(data.name).then(res => {
      EventBus.emit(Config.Event.refreshCategoryList);
      emit("update:show", false);
      showAlert("创建成功", "SUCCESS");
    });
  } else {
    //put修改
    Service.Shop.changeCategory(data).then(res => {
      EventBus.emit(Config.Event.refreshCategoryList);
      emit("update:show", false);
      showAlert("修改成功", "SUCCESS");
    });
  }
};
const cancel = () => {
  emit("update:show", false);
};
</script>
